package com.ly.heart.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.ly.common.utils.DateUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.dto.BranchSubTypeDto;
import com.ly.heart.domain.response.BranchSubTypeResponse;
import org.springframework.stereotype.Service;
import com.ly.heart.mapper.BranchSubTypeMapper;
import com.ly.heart.domain.BranchSubTypeEntity;


import javax.annotation.Resource;
import com.ly.heart.service.IBranchSubTypeService;

/**
 * 施工分部分项划分Service业务层处理
 * 
 * @author wutao
 * @date 2024-06-10
 */
@Service
public class BranchSubTypeServiceImpl extends ServiceImpl<BranchSubTypeMapper,BranchSubTypeEntity> implements IBranchSubTypeService {

    @Resource
    private BranchSubTypeMapper branchSubTypeMapper;

    /**
     * 查询施工分部分项划分
     * 
     * @param id 施工分部分项划分ID
     * @return 施工分部分项划分
     */
    @Override
    public BranchSubTypeEntity selectBranchSubTypeById(String id)
    {
        return branchSubTypeMapper.selectBranchSubTypeById(id);
    }

    /**
     * 查询施工分部分项划分列表
     * 
     * @param branchSubType 施工分部分项划分
     * @return 施工分部分项划分
     */
    @Override
    public  BranchSubTypeResponse selectBranchSubTypeList(BranchSubTypeDto branchSubType)
    {
        IPage<BranchSubTypeEntity> branchSubTypeEntityIPage = branchSubTypeMapper.selectBranchSubTypeList(branchSubType);
        List<BranchSubTypeEntity> records = branchSubTypeEntityIPage.getRecords();
        List<JSONObject> result = records.stream()
                .collect(Collectors.groupingBy(BranchSubTypeEntity::getParentBranchName))
                .entrySet().stream()
                .map(parentEntry -> {


                    JSONObject parentJson = new JSONObject();
                    parentJson.put("name", parentEntry.getKey());
                    List<JSONObject> childrenJsonList = parentEntry.getValue().stream()
                            .collect(Collectors.groupingBy(BranchSubTypeEntity::getSonBranchName))
                            .entrySet().stream()
                            .map(childEntry -> {
                                JSONObject childJson = new JSONObject();
                                childJson.put("name", childEntry.getKey());
                                childJson.put("children", childEntry.getValue()); // 直接将实体列表作为"children"
                                return childJson;
                            })
                            .collect(Collectors.toList());

                    parentJson.put("children", childrenJsonList);
                    return parentJson;
                })
                .collect(Collectors.toList());
        BranchSubTypeResponse branchSubTypeResponse = new BranchSubTypeResponse();
        branchSubTypeResponse.setRecords(result);
        branchSubTypeResponse.setTotal(branchSubTypeEntityIPage.getTotal());
        branchSubTypeResponse.setPage(branchSubTypeEntityIPage.getCurrent());
        branchSubTypeResponse.setLimit(branchSubTypeEntityIPage.getSize());
        branchSubTypeResponse.setPages(branchSubTypeEntityIPage.getPages());
        return branchSubTypeResponse;
    }



    // 创建表示子节点的JSONObject
    private JSONObject createChildJsonObject(Map.Entry<String, List<BranchSubTypeEntity>> childGroup) {
        JSONObject childJson = new JSONObject();
        childJson.put("name", childGroup.getKey());
        // 注意：此处直接将实体列表作为"children"，在某些场景下可能需要进一步处理这些实体
        childJson.put("children", childGroup.getValue());
        return childJson;
    }




    /**
     * 新增施工分部分项划分
     * 
     * @param branchSubType 施工分部分项划分
     * @return 结果
     */
    @Override
    public int insertBranchSubType(BranchSubTypeEntity branchSubType)
    {
        branchSubType.setCreateTime(DateUtils.getNowDate());
        branchSubType.setId(SnowflakeIdWorker.getInstance().nextStringId());
        return branchSubTypeMapper.insertBranchSubType(branchSubType);
    }

    /**
     * 修改施工分部分项划分
     * 
     * @param branchSubType 施工分部分项划分
     * @return 结果
     */
    @Override
    public int updateBranchSubType(BranchSubTypeEntity branchSubType)
    {
        branchSubType.setUpdateTime(DateUtils.getNowDate());
        return branchSubTypeMapper.updateBranchSubType(branchSubType);
    }

    /**
     * 批量删除施工分部分项划分
     * 
     * @param ids 需要删除的施工分部分项划分ID
     * @return 结果
     */
    @Override
    public int deleteBranchSubTypeByIds(String[] ids)
    {
        return branchSubTypeMapper.deleteBranchSubTypeByIds(ids);
    }

    /**
     * 删除施工分部分项划分信息
     * 
     * @param id 施工分部分项划分ID
     * @return 结果
     */
    @Override
    public int deleteBranchSubTypeById(String id)
    {
        return branchSubTypeMapper.deleteBranchSubTypeById(id);
    }
}
